var callBackReturn = false,
    OnPost = false;

layui
    .config({
        base: "../layui/exts/", //静态资源所在路径
    })
    .use(["form"], function () {
        var form = layui.form;
        var $ = layui.$;
        var index = parent.layer.getFrameIndex(window.name);

        form.render(null, 'postData');

        changeDatasrcModel(document.getElementById('datasrcModel').value, dataSrcCorr);

        form.render('select');
        changeIssort(document.getElementById('isSort').checked, document.getElementById('ismixSort').checked);

        /**选择字段**/
        form.on('select(fieldName)', function (data) {
            $.ajax({
                url: './selectField?&classifyId=' + classifyId + '&fillingModel=' + fillingModel, //url
                dataType: 'json', //数据格式
                type: 'POST', //请求方式
                async: false, //是否异步请求
                data: {id: ID, value: data.value},
                success: function (result) {
                    if (result.code == 0) {
                        data = result.data;
                        $('#fieldTitle').val(data.title);
                        let dataType = document.getElementById('dataType');
                        dataType[data.dbType - 1].selected = true;
                        let datasrcModel = document.getElementById('datasrcModel');
                        datasrcModel[data.defDataModel - 1].selected = true;
                        $('#dataLen').val(data.defLen);
                        $('#showWidth').val(data.defShowWidth);
                        $('#issys').val(data.issys);
                        form.render('select');
                        changeDatasrcModel(data.defDataModel, data.defSrc);
                        $('#datasrcCorresponding').val(data.defSrc);
                    } else {
                        let fn = document.getElementById('fieldName');
                        fn[0].selected = true;
                        form.render('select');
                        parent.layer.alert(result.message, {icon: 2});
                    }
                },
            });
        });

        /**选择字段**/
        form.on('select(datasrcModel)', function (data) {
            changeDatasrcModel(data.value, 0);
        });

        form.on('submit(postData)', function (obj) {
            var data = obj.field;
            data["title"] = $("#fieldName").find("option:selected").text();
            data["datasrcName"] = $("#datasrcCorresponding").find("option:selected").text();
            $.ajax({
                url: './saveFieldConfig', //url
                contentType: "application/json",
                dataType: 'json', //数据格式
                type: 'POST', //请求方式
                async: false, //是否异步请求
                data: JSON.stringify(data),
                success: function (result) {
                    if (result.code == 0) {
                        parent.layer.msg(result.message, {icon: 1});
                        callBackReturn = true;
                        OnPost = true;
                        parent.layer.close(index);
                    } else {
                        OnPost = true;
                        parent.layer.alert(result.message, {icon: 2});
                    }
                },
            });
            return false;
        });

        form.on('checkbox(issort)', function (data) {
            changeIssort(data.elem.checked, document.getElementById('ismixSort').checked);
        });
        form.on('checkbox(ismixSort)', function (data) {
            changeIssort(document.getElementById('issort').checked, data.elem.checked);
        });

        function changeIssort(value1, value2) {
            $("#sortIdx").removeAttr("lay-verify");
            $("#sortIdx").attr("disabled", "disabled")
            if (value1 == true || value2 == true) {
                $("#sortIdx").removeAttr("disabled");
                $("#sortIdx").attr("lay-verify", "number");
            }
        }

        function changeDatasrcModel(value, def) {
            let dataSrc = document.getElementById('datasrcCorresponding');
            $(dataSrc).removeAttr("disabled");
            $(dataSrc).attr("lay-verify", "required");
            $(dataSrc).html('');
            $(dataSrc).append('<option value=""></option>');
            switch (value) {
                case "2":
                    $(dataSrc).append('<option value="1">当前用户</option>');
                    $(dataSrc).append('<option value="2">当前时间</option>');
                    $(dataSrc).append('<option value="3">分类号</option>');
                    $(dataSrc).append('<option value="4">分类名称</option>');
                    if (def > 0) {
                        dataSrc[def].selected = true;
                    }
                    break;
                case "4":
                    //上级数据
                    let model = Number(fillingModel) + 1;
                    $.ajax({
                        url: './getParentField?classifyId=' + classifyId + "&fillingModel=" + model, //url
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: {},
                        success: function (result) {
                            if (result.code == 0) {
                                let d = result.data;
                                for (let key in result.data) {
                                    if (def == key) {
                                        $(dataSrc).append('<option value=' + key + ' selected>' + d[key] + '</option>');
                                    } else {
                                        $(dataSrc).append('<option value=' + key + '>' + d[key] + '</option>');
                                    }

                                }
                            } else {
                                parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    break;
                case "3":
                    $.ajax({
                        url: '../basic/getDictionaryClassData?model=1', //url
                        dataType: 'json', //数据格式
                        type: 'GET', //请求方式
                        async: false, //是否异步请求
                        data: {},
                        success: function (result) {
                            if (result.code == 0) {
                                for (let i = 0; i < result.data.length; i++) {
                                    if (def == result.data[i].id) {
                                        $(dataSrc).append('<option value=' + result.data[i].id
                                            + ' selected >' + result.data[i].name + '</option>');
                                    } else {
                                        $(dataSrc).append('<option value=' + result.data[i].id
                                            + '>' + result.data[i].name + '</option>');
                                    }
                                }
                            } else {
                                parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    break;
                case "5":
                    $(dataSrc).append('<option value="1">文件页数</option>');
                    $(dataSrc).append('<option value="2">文件件数</option>');
                    $(dataSrc).append('<option value="6">统计页数</option>');
                    $(dataSrc).append('<option value="3">文件起始日期</option>');
                    $(dataSrc).append('<option value="4">文件结束日期</option>');
                    $(dataSrc).append('<option value="5">最高密级</option>');
                    if (def > 0) {
                        dataSrc[def].selected = true;
                    }
                    break;
                default:
                    $(dataSrc).attr("disabled", "disabled")
                    $(dataSrc).removeAttr("lay-verify");
            }
            form.render('select');
        }

    });

function Buttun_Submit() {
    document.getElementById('postBut').click();
    return callBackReturn;
}